/**
 * Copyright 2008 Mathias Doenitz, http://lis.to/
 *
 * This file is part of the lis.to java desktop client. The lis.to java desktop client is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The lis.to java desktop client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with the lis.to java desktop client.
 * If not, see http://www.gnu.org/licenses/
 */

package listo.client.tasklists;

import com.google.inject.Inject;
import listo.client.ContextManager;
import listo.client.Convert;
import listo.client.MenuBuilder;
import listo.client.actions.Actions;
import listo.client.dragdrop.TaskListTransferHandler;
import listo.client.model.Tasks;
import listo.client.model.operations.RepositionTasksOp;
import listo.client.taskfilters.SelectedViewFilter;
import listo.client.taskfilters.TaskFilter;
import listo.utils.MiscUtils;

import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

public class MainFormTaskList extends TaskList implements TaskFilter.Listener {

    private final TaskListManager taskListManager;
    private final SelectedViewFilter filter;
    private final ContextManager contextManager;

    @Inject
    public MainFormTaskList(final TaskListRenderer renderer, Actions actions, MenuBuilder menuBuilder,
                            TaskListTransferHandler transferHandler, TaskListManager taskListManager,
                            SelectedViewFilter filter, ContextManager contextManager) {
        super(renderer, actions, menuBuilder, taskListManager);
        this.taskListManager = taskListManager;
        this.filter = filter;
        this.contextManager = contextManager;

        setDragEnabled(true);
        setDropMode(DropMode.ON_OR_INSERT);
        setTransferHandler(transferHandler);

        taskListManager.register(this);
        filter.addFilterListener(this);

        registerKeyboardAction(actions.editTaskAction, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
                JComponent.WHEN_FOCUSED);

        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if (!e.isConsumed() && e.isAltDown() &&
                        (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN)) {
                    Tasks selTasks = new Tasks(getSelectedValues());
                    int[] indices = indicesOf(selTasks);
                    int minIx = MiscUtils.min(indices);
                    if (e.getKeyCode() == KeyEvent.VK_UP && minIx == 0) return;
                    if (e.getKeyCode() == KeyEvent.VK_DOWN &&
                            MiscUtils.max(indices) == MainFormTaskList.this.tasks.size() - 1) {
                        return;
                    }
                    RepositionTasksOp op = new RepositionTasksOp();
                    op.setTasks(Convert.toObjectIds(selTasks.toArray()));
                    op.setPosition(MainFormTaskList.this.contextManager.getCurrentContext().indexOf(
                            MainFormTaskList.this.tasks.get(minIx + (e.getKeyCode() == KeyEvent.VK_UP ? -1 : 1))));
                    MainFormTaskList.this.contextManager.addAndRun(op);
                }
            }
        });
    }

    public void tasksChanged(Object updateObject) {
        if (taskListManager.getActiveTaskList() == this) {
            setTasks((Tasks) updateObject);
        }
    }

    protected void createPopupMenu(MouseEvent e) {
        if (!filter.isTrash()) {
            createPopupMenu(e,
                    actions.toggleTasksCompletedAction,
                    actions.editTaskAction,
                    actions.detachCurrentFoldersAction,
                    actions.deleteTasksAction,
                    null,
                    actions.addTaskAction
            );
        }
    }

}
